﻿using System.Collections.Generic;
using System.Threading;
using DimensionsHelper.Common.Contracts.TableFormatter.Options;
using DimensionsHelper.Common.Contracts.Excel;

namespace DimensionsHelper.Common.Contracts.TableFormatter;

public interface ITableFormatter : IProjectBase<TableFormatterOptions>
{
    /// <summary>
    ///     对单一文件执行格式化器。
    /// </summary>
    /// <param name="destination">已打开的电子表格</param>
    /// <param name="inputFileName">输入文件</param>
    void Execute(IExcelWorkbookOperator destination, string inputFileName);

    /// <summary>
    ///     对多个文件执行格式化器。
    /// </summary>
    /// <param name="destination">已打开的电子表格</param>
    /// <param name="inputFileNames">输入文件列表</param>
    void Execute(IExcelWorkbookOperator destination, IEnumerable<string> inputFileNames);

    /// <summary>
    ///     对多个文件执行格式化器，并且允许中途取消。
    /// </summary>
    /// <param name="destination">已打开的电子表格</param>
    /// <param name="inputFileNames">输入文件列表</param>
    /// <param name="cancellationToken">给定进程的<see cref="CancellationToken" /></param>
    void Execute(IExcelWorkbookOperator destination, IEnumerable<string> inputFileNames,
        CancellationToken cancellationToken);

    /// <summary>
    ///     对多个文件指定格式化器，并且对于每个文件插入指定的文件，允许中途取消。
    /// </summary>
    /// <param name="destination">已打开的电子表格</param>
    /// <param name="inputFiles">输入文件列表</param>
    /// <param name="cancellationToken"></param>
    void Execute(IExcelWorkbookOperator destination, IEnumerable<TableInputFileInfo> inputFiles,
        CancellationToken cancellationToken);
}


/// <summary>
///     插入Csv文件的信息
/// </summary>
public struct TableInsertFileInfo
{
    /// <summary>
    ///     插入行号
    /// </summary>
    public int? LineNumber;

    /// <summary>
    ///     插入表格名
    /// </summary>
    public string? TableName;

    /// <summary>
    ///     插入文件名
    /// </summary>
    public string FileName;

    /// <summary>
    ///     空的Csv插入信息，也应当是默认值。
    /// </summary>
    public static readonly TableInsertFileInfo Empty = new()
    {
        LineNumber = null,
        TableName = null,
        FileName = string.Empty
    };

    /// <summary>
    ///     如果给定的插入文件信息是有效的，返回<see langword="true"/>
    /// </summary>
    public static bool IsValid(in TableInsertFileInfo fileInfo)
    {
        return !string.IsNullOrEmpty(fileInfo.FileName) && (fileInfo.LineNumber != null || !string.IsNullOrEmpty(fileInfo.TableName));
    }
}


public struct TableReplaceFileInfo
{
    /// <summary>
    ///     替换表格文件的文件名
    /// </summary>
    public string FileName;

    /// <summary>
    ///     如果按照表格名称替换，设置为<see langword="true" />
    /// </summary>
    public bool ByName;

    /// <summary>
    ///     如果按照表格索引替换，设置为<see langword="true" />
    /// </summary>
    public bool ByIndex;


    public static readonly TableReplaceFileInfo Empty = new()
    {
        FileName = string.Empty,
        ByName = false,
        ByIndex = false
    };


    /// <summary>
    ///     如果给定的替换信息有效，返回<see langword="true"/>。
    /// </summary>
    public static bool IsValid(in TableReplaceFileInfo fileInfo)
    {
        return !string.IsNullOrEmpty(fileInfo.FileName) && (fileInfo.ByName || fileInfo.ByIndex);
    }
}


/// <summary>
///     Csv输入文件信息，包含插入的文件信息
/// </summary>
public class TableInputFileInfo(
    string fileName,
    IReadOnlyList<TableInsertFileInfo> insertFiles,
    IReadOnlyList<TableReplaceFileInfo> replaceFiles)
{
    public string FileName { get; } = fileName;

    public IReadOnlyList<TableInsertFileInfo> InsertFiles { get; } = insertFiles;

    public IReadOnlyList<TableReplaceFileInfo> ReplaceFiles { get; } = replaceFiles;
}